package com.financing.wap.service.impl;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.wap.common.ResultMap;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dto.SwitchDTO;
import com.financing.wap.service.ICheckCodeService;
import com.financing.wap.service.ISmsService;
import com.financing.wap.service.ISystemService;
import com.financing.wap.util.DateUtil;
import com.financing.wap.util.PropertiesUtils;
import com.financing.wap.util.StringUtil;

@Service("checkCodeServiceImpl")
public class CheckCodeServiceImpl implements ICheckCodeService{
private static final Logger log = LoggerFactory.getLogger(ClientServiceImpl.class);
	
	/**
	 * 开关
	 */
	private static String SWITCH_TURN_ON = "1";
	
	@Autowired
	public ISystemService systemService;
	
	@Override
	public boolean CheckCodeSwitch() {
		try{
			SwitchDTO switchDTO = systemService.getSwitch("CHECK_CODE_SWITCH");
			if(switchDTO != null && SWITCH_TURN_ON.equals(switchDTO.getValue())){
				log.info("sms switch is on!");
				return true;
			}
		}catch(Exception e){
			log.error("error when query sms switch", e);
			return false;
		}
		log.info("sms switch is off!");
		return false;
	}

	@Override
	public ResultMap checkOtpCode(String mobile, String vCode) {
		log.info("-------------begin checkOtpCode		----------------");
		ResultMap result = new ResultMap();
		//获取超时分钟数
		Integer activety_otp_time= new Integer(PropertiesUtils.getValue("activety_otp_time"));
		//短信时间倒计时(首次)
		String timeOutType = ISmsService.OTP_CODE_FIRST_TIME;
		//获取 验证码时间
		Date otpCodeDateTime = getOtpCodeTime(mobile,timeOutType);
		
		//参数判断
		if(StringUtil.isNullOrEmptyStr(mobile) || StringUtil.isNullOrEmptyStr(vCode)){
			result.setStatus(ResultMap.STATUS.FAILE);
			result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
			result.setMessage("参数缺失");
			return result;
		}
		//如果等于空 就返回OTP校验失败 、不一致 返回OTP校验失败
		if(otpCodeDateTime==null || !vCode.equalsIgnoreCase(getOtpCode(mobile))){
			result.setStatus(ResultMap.STATUS.FAILE);
			result.setStatusCode(ResultMap.STATUS_CODE_OTP_VALIDATE_ERROR);
			result.setMessage("手机验证码不正确！");
			log.info("-------------otpDateTime or vCode error---------------");
			log.info("-------------end  checkOtpCode        ----------------");
			return result;
		}
		//验证码是否超时
		if(DateUtil.timePlusMinutesComparedNow(otpCodeDateTime,activety_otp_time)){
			result.setStatus(ResultMap.STATUS.FAILE);
			result.setStatusCode(ResultMap.STATUS_CODE_OTP_VALIDATE_ERROR_TIMEOUT);
			result.setMessage("手机验证码超时！");
			log.info("-------------first otpDateTime is null----------------");
			log.info("-------------end  checkOtpCode        ----------------");
			return result;
		}
		//返回成功标记
			result.setStatus(ResultMap.STATUS.SUCCESS);
			log.info("-------------"+result.getStatus()+"----------------");
			log.info("-------------end  checkOtpCode     ----------------");
		return result;
	}
	
	/**
	 * 获取otp验证码时间
	 * @return
	 */
	private  Date getOtpCodeTime(String ID,String timeOutType){
		if(ISmsService.OTP_CODE_FIRST_TIME.equals(timeOutType)){
			return (Date) SessionUtil.getSession().getAttribute(ISmsService.OTP_CODE_FIRST_TIME + ID);
		}if(ISmsService.OTP_CODE_LAST_TIME.equals(timeOutType)){
			return (Date) SessionUtil.getSession().getAttribute(ISmsService.OTP_CODE_LAST_TIME + ID);
		}
		return null;
	}
	
	/**
	 * 获取otp验证码
	 * @return
	 */
	private  String getOtpCode(String ID){
		return (String) SessionUtil.getSession().getAttribute(ISmsService.OTP_CODE + ID);
	}
	
}
